//
//  LGAuthorityManager.m
//  LuxuryGoods
//
//  Created by 刘星辰 on 2018/5/19.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "LGAuthorityManager.h"
#import  <AddressBook/AddressBook.h>
#import <Photos/Photos.h>
#import <CoreLocation/CoreLocation.h>
@implementation LGAuthorityManager
/**
 请求通讯录权限
 */
+ (void)requestAddressBookAuthority:(void(^)(NSError *error))handle
{
    __block ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        if (granted) {
            // 成功
            if (handle)
            {
                handle(nil);
            }
        } else {
            // 失败
            if (handle)
            {
                handle([NSError errorWithDomain:@"通讯录权限未打开" code:400 userInfo:nil]);
            }
        }
        if (addressBook) {
            CFRelease(addressBook);
            addressBook = NULL;
        }
    });
}

/**
 请求相册权限
 */
+ (void)requestPhotoLibraryAuthority:(void(^)(NSError *error))handle
{
    // 判断授权状态
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        dispatch_sync(dispatch_get_main_queue(), ^{
            if (status != PHAuthorizationStatusAuthorized)
            {
                if (handle)
                {
                    handle([NSError errorWithDomain:@"相册权限未开启" code:400 userInfo:nil]);
                }
            }
            else
            {
                if (handle)
                {
                    handle(nil);
                }
            }
        });
        
    }];
}

+ (void)requestAudioRecordAuthority:(void(^)(NSError *error))handle
{
    AVAudioSessionRecordPermission permissionStatus = [[AVAudioSession sharedInstance] recordPermission];
    
    switch (permissionStatus) {
        case AVAudioSessionRecordPermissionUndetermined:{
            [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
                // CALL YOUR METHOD HERE - as this assumes being called only once from user interacting with permission alert!
                if (granted) {
                    // Microphone enabled code
                    if (handle)
                    {
                        handle(nil);
                    }
                }
                else {
                    // Microphone disabled code
                    if (handle)
                    {
                        handle([NSError errorWithDomain:@"麦克风权限未开启" code:400 userInfo:nil]);
                    }
                }
            }];
            break;
        }
        case AVAudioSessionRecordPermissionDenied:
            // direct to settings...
            //            NSLog(@"已经拒绝麦克风弹框");
            if (handle)
            {
                handle([NSError errorWithDomain:@"麦克风权限未开启" code:400 userInfo:nil]);
            }
            break;
        case AVAudioSessionRecordPermissionGranted:
            NSLog(@"已经允许麦克风弹框");
            // mic access ok...
            if (handle)
            {
                handle(nil);
            }
            break;
        default:
            // this should not happen.. maybe throw an exception.
            if (handle)
            {
                handle([NSError errorWithDomain:@"麦克风权限未开启" code:400 userInfo:nil]);
            }
            break;
    }
    //    if(permissionStatus == AVAudioSessionRecordPermissionUndetermined) return;
    
}

/**
 请求定位权限
 */
+ (void)requestLocationAuthority:(void(^)(NSError *error))handle
{
    if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined))
    {
        
        if (handle)
        {
            handle(nil);
        }
        return;
        
    }
    else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted)
    {
        if (handle)
        {
            handle([NSError errorWithDomain:@"定位权限未开启" code:400 userInfo:nil]);
        }
        return;
    }
    if (handle)
    {
        handle([NSError errorWithDomain:@"未知错误" code:400 userInfo:nil]);
    }
}

/**
 相机权限
 */
+ (void)requestCameraAuthority:(void (^)(NSError *error))handle
{
    /// 先判断摄像头硬件是否好用
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        // 用户是否允许摄像头使用
        NSString * mediaType = AVMediaTypeVideo;
        AVAuthorizationStatus  authorizationStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
        // 不允许弹出提示框
        if (authorizationStatus == AVAuthorizationStatusRestricted|| authorizationStatus == AVAuthorizationStatusDenied)
        {
            if (handle)
            {
                handle([NSError errorWithDomain:@"相机权限未开启" code:400 userInfo:nil]);
            }
        }
        else
        {
            // 这里是摄像头可以使用的处理逻辑
            if (handle)
            {
                handle(nil);
            }
        }
    }
    else
    {
        // 硬件问题提示
        if (handle)
        {
            handle([NSError errorWithDomain:@"相机硬件问题" code:400 userInfo:nil]);
        }
    }
}

@end
